<template>
    <el-container>
        <el-header class="header-tabs">
            <el-tabs type="card" v-model="groupId" @tab-change="tabChange">
                <el-tab-pane label="全部" name="-1">
                    <template #label>
                        <el-badge :value="headnumber.total" class="item" :max="9999">
                            <div style="width:40px">全部</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="待审核" name="0">
                    <template #label>
                        <el-badge :value="headnumber.uncheck" class="item" :max="9999">
                            <div style="width:50px">待审核</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="已审核" name="1">
                    <template #label>
                        <el-badge :value="headnumber.check" class="item" :max="9999">
                            <div style="width:50px">已审核</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </el-header>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'operate.add'"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'operate.delete'"></el-button>
                <el-button type="warning" @click="batch_export" :disabled="selection.length != 1" v-auth="'operate.export'">导出</el-button>
                <!-- <sc-file-import :apiObj="$API.order.entrust.orderUp" @success="success" ref="importBox" templateUrl="https://2156.mzth.cn/import.xlsx"></sc-file-import> -->
                <div class="finish">
                    <div class="finishNum" v-if="groupId == 0">未核销金额合计:<div class="text">{{headnumber.not_sub_money}}</div></div>
                    <div class="finishNum" v-if="groupId == 1">已核销金额合计:<div class="text">{{headnumber.sub_money}}</div></div>
                </div>
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <el-input style="margin:5px 10px" v-model="search.operate_code" placeholder="请输入报销编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.hotel_id" placeholder="请选择所属酒店" clearable filterable>
                        <el-option v-for="item in hotelList" :key="item.id" :label="item.hotel_name" :value="item.id"/>
                    </el-select>
                    <el-date-picker v-model="search.create_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="报销单号" prop="operate_code" align="center" width="120"></el-table-column>
                <el-table-column label="提报人" prop="admin.name" align="center" width="120"></el-table-column>
                <el-table-column label="报销类型" prop="operate_detail[0].operate_type" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.operate_detail!=''">
                            <div v-for="item in scope.row.operate_detail" :key="item.id">
                                <div v-for="item1 in operateList" :key="item1.value">
                                    {{item.operate_type == item1.value ? item1.name : ''}}
                                </div>
                            </div>
                            <!-- <div v-if="scope.row.operate_detail[0].operate_type==1">季节性报销</div>
                            <div v-if="scope.row.operate_detail[0].operate_type==2">办公用品报销</div>
                            <div v-if="scope.row.operate_detail[0].operate_type==3">公车加油报销</div>
                            <div v-if="scope.row.operate_detail[0].operate_type==4">公关应酬报销</div>
                            <div v-if="scope.row.operate_detail[0].operate_type==5">物料损耗报销</div>
                            <div v-if="scope.row.operate_detail[0].operate_type==6">仓库运营报销</div> -->
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="报销详情" prop="operate_detail[0].detail" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.operate_detail!=''">
                            <div v-for="item in scope.row.operate_detail" :key="item.id">
                                {{item.detail}}
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="报销金额合计" prop="money_all" align="center" width="120"></el-table-column>
                <el-table-column label="酒店" prop="hotel.hotel_name" align="center" width="150"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center" width="120"></el-table-column>
                <!-- <el-table-column label="附件" prop="withProfession.title" align="center">
                    <template #default="scope">
                        <el-image class="img" :src="scope.row.goods_image" :preview-src-list="scope.row.goods_image_item" fit="cover" preview-teleported="true" hide-on-click-modal />
                    </template>
                </el-table-column> -->
                <el-table-column label="审核状态" prop="status" align="center" width="120">
                    <template #default="scope">
                        <el-tag :type="scope.row.status == 0 ?'warning' : scope.row.status == 1 ? 'success' : 'danger'">{{scope.row.status == 0 ?'待审核' : scope.row.status == 1 ? '审核通过' : '审核拒绝'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="提报日期" prop="create_time" align="center" width="170"></el-table-column>
                <el-table-column label="操作" align="center" width="160">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'operate.show'">查看</el-button>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'operate.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'operate.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button text type="warning" size="small" @click="table_check(scope.row, scope.$index)" v-auth="'operate.check'" v-if="scope.row.status == 0">审核</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <check-status v-if="dialog.status" ref="statusDialog" @success="handleSuccess" @closed="dialog.status = false"></check-status>
    <el-dialog v-model="dialog.export" title="酒店" width="350" :before-close="handleClose" align-center>
        <el-form-item label="请选择酒店" prop="has_company">
            <el-select v-model="hotel_id" placeholder="请选择所属酒店" style="width:100%" clearable filterable @change="hotelChange">
                <el-option v-for="item in hotelList" :key="item.id" :label="item.hotel_name" :value="item.id"/>
            </el-select>
        </el-form-item>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialog.export = false">取消</el-button>
                <el-button type="primary" @click="batch_export" :loading="isSaveing">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script>
import CheckStatus from '../checkStatus.vue'
import saveDialog from './save.vue'

export default {
	name: '客户管理',
	components: {
		saveDialog,CheckStatus,
	},
	data() {
		return {
			dialog: {
				save: false,
				status: false,
				export: false,
			},
			apiObj: this.$API.dispatch.operate.list,
			selection: [],
			search: {
				dispatch_code: null,
				create_time: null,
				status: null,
			},
            groupId:"-1",
            headnumber:{},
            hotel_id: "",
            hotel_name: "",
            hotelList: [],
            operateList: [],
		}
	},
	mounted() {
        this.getNum()
        this.getHotel()
        this.getOperate()
	},
	methods: {
        success(res){
			if(res.code==1){
				this.$refs.table.refresh()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
        // 导出
        async batch_export(){
            let query = {
                id:this.selection[0].id,
                hotel_name:this.selection[0].hotel&&this.selection[0].hotel.hotel_name
            }
            var res = await this.$API.dispatch.operate.export.post(query)
            if (res.code == 1) {
                this.dialog.export = false
                this.hotel_id = ""
                const fileUrl = res.data.url;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = fileUrl;
                link.click();
            } else {
                this.$alert(res.message, "提示", { type: "error" });
            }
        },
        // 审核
        table_check(row) {
			this.dialog.status = true
			this.$nextTick(() => {
				this.$refs.statusDialog.open('operate').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.dispatch.operate.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
                this.getNum()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.dispatch.operate.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
                this.getNum()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'item') {
				this.$refs.table.refresh()
			} else if (mode == 'operate') {
				this.$refs.table.refresh()
			}
            this.getNum()
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.groupId = ""
            this.$refs.table.upData(this.search)
        },
        tabChange(e){
            this.groupId = e
            if(e != -1){
                this.search.status = e
            }else{
                this.search.status = null
            }
            this.upsearch()
        },
        async getNum(){
            var res = await this.$API.dispatch.operate.list.get();
            this.headnumber = res.data.extend
        },
        async getOperate(){
            var res = await this.$API.common.getDictData.get({
				type: 'operate'
			});
			let array = []
			res.data.operate.map(item => {
				array.push({
					name: item.name,
					value: Number(item.value)
				})
			})
			this.operateList = array
        },
        async getHotel(){
            var res = await this.$API.client.hotel.list.get();
            this.hotelList = res.data.rows;
        },
        hotelChange(e){
            this.hotelList.map(item=>{
                if(item.id == e){
                    this.hotel_name = item.hotel_name
                }
            })
        },
	}
}
</script>

<style>
.titleBox {
	font-size: 14px;
	font-weight: 500;
	color: #0099ff;
	margin-left: 30px;
}
</style>
